<template>
	<view class="button-sp-area">
		<button v-if="!openApp" class="bottom" type="primary" @tap="getUserProfile">点击绑定</button>

		<button v-else type="primary" class="bottom" open-type="launchApp" @error="launchAppError"
			@launchapp="returnIndex">绑定成功，点击返回</button>
		<private_tip :privateShow="privateb"></private_tip>
	</view>
</template>

<script>
	//获取应用实例
	const app = getApp();
	var _this;
	export default {
		data() {
			return {
				privateb: false,
				//判断小程序的API，回调，参数，组件等是否在当前版本可用。
				canIUse: uni.canIUse('button.open-type.getUserInfo'),

				// canIUse:false,
				canIUseGetUserProfile: false,

				masterDomain: app.globalData.masterDomain,
				qishouid: '13',
				openApp: false,
				code: '',
				userData: ''
			};
		},
		onLoad: function(options) {
			_this = this;
			if (app.globalData.wxprivate == -1) {
				app.watchG('wxprivate', function(value) {
					_this.wxprivate = value;
					if (value) { //弹出弹窗
						_this.privateb = true;
					} else {
						_this.onload(options);
					}
				})
			} else {
				if (app.globalData.wxprivate) { //弹出弹窗
					_this.privateb = true;
				} else {
					_this.onload(options);
				}
			}

		},
		onShareAppMessage(res) {
			let config = app.globalData.appConfig;
			return {
				title: config.share.title,
				desc: config.share.description,
				path: '/pages/index/index',
			};
		},
		// 分享到朋友圈
		onShareTimeline() {
			return {
				title: config.share.title,
			};
		},
		methods: {
			onload(options) {
				_this.qishouid = options.qishouid;
			},
			launchAppError(e) {
				console.log(e);
			},

			// 成功返回app
			returnIndex(e) {
				uni.redirectTo({
					url: '/pages/index/index'
				});
			},

			bindGetUserInfo: function(e) {
				var _appid = this._appid;
				var _path = this._url;
				if (_path && _path.indexOf('huoniao/') > -1) {
					_path = _path.replace('huoniao/', '/pages/redirect/index?url=/');
				}
				_path = _path == '' ? '' : _path;
				uni.navigateToMiniProgram({
					appId: _appid,
					path: _path,
					success(res) {
						uni.navigateBack();
					},
					fail(res) {
						uni.showToast({
							title: res,
							icon: 'success',
							duration: 2000
						});
					}
				});
			},

			getUserProfile(e) {
				// 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认
				// 开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
				let code = '';
				uni.login({
					success: (res) => {
						code = res.code;
						_this.code = res.code
					}
				});
				uni.getUserProfile({
					lang: 'zh_CN',
					desc: '用于完善会员资料',
					// 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
					success: (res) => {
						var userData = {
							code: _this.code,
							iv: res.iv,
							encryptedData: res.encryptedData,
							// redirect: that.redirect,
							nickname: res.userInfo.nickName,
							photo: res.userInfo.avatarUrl,
							city: res.userInfo.city,
							province: res.userInfo.province,
							gender: res.userInfo.gender
						};
						_this.userData = userData
						_this.canIUse = false

						// 存储本地
						uni.setStorage({
							key: 'applyUserInfo',
							data: 1
						});
						_this.bindQishouId();
					},
					fail: () => {
						uni.showToast({
							title: '已拒绝小程序获取信息',
							icon: 'none',
							duration: 2000
						});
					}
				});
			},

			// 返回数据给后台
			bindQishouId() {
				var tt = this;
				tt.$ajax.post({
					url: 'include/ajax.php?service=waimai&action=waimaiCourierOpenid',
					data: {
						did: tt.qishouid,
						code: tt.code
					},
				}).then(res => {
					if (res.data) {
						var data = res.data;
						if (data.state == 100) {
							uni.showToast({
								title: data.info,
								icon: 'none',
								duration: 2000
							});
							_this.openApp = true
						} else {
							uni.hideLoading();
							// uni.showToast({
							// 	title: data.info + ',' + tt.qishouid,
							// 	icon: 'none',
							// 	duration: 2000
							// });
							uni.showModal({
								title:'温馨提示',
								content:data.info + ',' + tt.qishouid,
								showCancel:false,
							})
						}
					}
				})
			}
		}
	};
</script>
<style>
	@import './index.css';
</style>